package com.sinoglobal.sinostore.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.SecondKillFragmentPagerAdapter;
import com.sinoglobal.sinostore.bean.ChildActivityList;
import com.sinoglobal.sinostore.bean.ChildActivityList.ChildActivityVo;
import com.sinoglobal.sinostore.bean.MiaoShaVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.widget.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
/**
 * 
* @title: SecondKillActivity.java 
* @package com.chiliring.sinostore.activity 
* @description: TODO(秒杀列表) 
* @author LK
* @date 2015-1-13 下午2:53:56
 */
public class SecondKillActivity extends StateBaseActivity {
	public static ArrayList<ChildActivityVo> childActivityVos;
	
	/**指示器**/
	private TabPageIndicator pageIndicator;
	
	private ViewPager viewPager;
	
	/**秒杀分时段**/
	private SecondKillFragmentPagerAdapter fragmentPagerAdapter;
	private String id;
	private MiaoShaVo miaoshaVo;
	private String activityId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		titleView.setText("限时秒杀");
		setContentView(R.layout.shop_activity_second_kill);
		openChenJInShi();
//		miaoshaVo=(MiaoShaVo) getIntent().getSerializableExtra(MiaoShaVo.class.getCanonicalName());
		id=getIntent().getStringExtra("id");
		activityId = getIntent().getStringExtra("activityId");
		initView();
		
		initData();
	}
	
	private void initView(){
		pageIndicator = (TabPageIndicator)this.findViewById(R.id.mPagerTabStrip);
		viewPager = (ViewPager)this.findViewById(R.id.mViewPager);
	}
	
	private void initData(){
		childActivityVos = new ArrayList<ChildActivityList.ChildActivityVo>();
		initPageIndicator();
	}
	
	/**
	 * 
	* @author LK
	* @methods initPageIndicator 
	* @description 描述信息 初始化指示器
	* @date 2015-1-13 下午4:46:51 参数说明
	 */
	private void initPageIndicator(){
		getActiveList();
		fragmentPagerAdapter = new SecondKillFragmentPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(fragmentPagerAdapter);
		pageIndicator.setViewPager(viewPager);
		  //如果我们要对ViewPager设置监听，用indicator设置就行了  
		pageIndicator.setOnPageChangeListener(new OnPageChangeListener() {  
              
            @Override  
            public void onPageSelected(int arg0) {  
            	
            }  
              
            @Override  
            public void onPageScrolled(int arg0, float arg1, int arg2) {  
                  
            }  
            @Override  
            public void onPageScrollStateChanged(int arg0) {  
                  
            }  
        });  
	}
	
	/**
	 * 
	* @author LK
	* @methods getActiveList 
	* @description 描述信息  获取子活动列表
	* @date 2015-1-19 上午11:43:48 参数说明
	 */
	private void getActiveList(){
		final ProgressDialog progressDialog=new ProgressDialog(this);
		progressDialog.setMessage("加载中...");
		try {
			progressDialog.show();
		} catch (Exception e) {
		}
		HttpUtils httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por","601");
		params.addQueryStringParameter("proIden",Constants.STOREFLAG);
		params.addQueryStringParameter("id",String.valueOf(id));
		ApiDebugUtil.debug(params);
		httpUtils.send(HttpMethod.POST, Constants.STORE_URL	, params, new RequestCallBack<String>() {
			@Override
			public void onStart() {
				super.onStart();
				LogUtils.i("......"+this.getRequestUrl());
			}
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}
			
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				String result = responseInfo.result;
				LogUtils.i("result="+result);
				if(!TextUtils.isEmpty(result)){
					ChildActivityList entity;
					try {
						entity = JSON.parseObject(result, ChildActivityList.class);
					} catch (Exception e) {
						e.printStackTrace();
						showShortToast("数据解析错误");
						return;
					}
					if(null != entity){
						if(Constants.STORE_SUCCESS_CODE.equals(entity.getCode())){
							childActivityVos = entity.list;
							fragmentPagerAdapter.setList(childActivityVos,entity.server_time);
							for(int i = 0;i < childActivityVos.size() ;i++){
								if(activityId.equals(childActivityVos.get(i).id)){
									viewPager.setCurrentItem(i, true);
									break;
								}
							}
							pageIndicator.notifyDataSetChanged();
						}
					}
				}
			}
		});
	}
}
